package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Environment;
import com.village.zpark.service.EnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/environment")
public class EnvironmentController {
    @Autowired
    private EnvironmentService environmentService;

    @RequestMapping
    public String login(){
        return "/environment";
    }

    @RequestMapping(value = "/environmentPage")
    public String environmentPage(){
        return "/environmentPage";
    }

    @RequestMapping(value = "/environmentQueryById")
    public Object EnvironmentQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Environment environment = environmentService.findById(id);
        model.addAttribute("obj",environment);
        return "/environmentPage";
    }

    @RequestMapping(value = "/environmentList")
    @ResponseBody
    public Object EnvironmentList(){
        List<Environment> environmentList = environmentService.findAll();
        return ResultMapUtil.getHashMapList(environmentList);
    }


    @RequestMapping(value = "/addenvironment")
    @ResponseBody
    public Object AddEnvironment(Environment environment){
        try {
            environment.setMonitoringtime(new Date());
            int i = environmentService.addEnvironment(environment);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updateenvironment")
    @ResponseBody
    public Object UpdateEnvironment(Environment environment){
        try {
            int i = environmentService.updateEnvironment(environment);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deleteenvironment")
    @ResponseBody
    public Object DeleteEnvironment(Integer id){
        try {
            int i = environmentService.deleteEnvironmentById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/environmentQueryPage")
    @ResponseBody
    public Map<String, Object> EnvironmentQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Environment> environmentPageInfo= environmentService.findPage(page, limit ,param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", environmentPageInfo.getTotal());
        map.put("data", environmentPageInfo.getList());
        return map;
    }
}
